MongoDB Add delete check and change operation example [Based on JavaScript Shell]

  • 2020-12-22 17:50:19
  • OfStack

An example of MongoDB is presented in this paper. To share for your reference, the details are as follows:

MongoDB comes with 1 JavaScript Shell, so it's ok to use the js syntax there.

Insert operation:

A single insert


var single={"name":"mei","age":22}
db.user.insert(single);

Circular insert


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

Find query operation:


db. A collection of .find(query,fields,limit,skip)

query, which specifies the query criteria, is equivalent to the where statement in SQL
Example:


db.student.find({"name":"joe","age":{$lt:22}})

fields for field mapping, syntax: {field:0} or {field:1}

Example:


db.student.find({"age":{$lt:22},{"_id":0,"name":1}})

Indicates that the query results contain the name field and not the _id field

limit limits the number of documents in the query result set, specifying an upper limit on the number of results returned by the query

Example:


db.student.find({"name":"joe"},{"name":1,"age":1},5)

skip skips the 1 fixed number of results and sets the offset of the 1 returned document

Example:


db.student.find({"name":"joe"},{"name":1,"age":1},5,20)

Means to skip the first 20 documents

Sorting: -1 descending, 1 ascending


db.user.findOne()

Note: MongoDB does not support join queries between multiple collections. The find function queries only one collection at a time

Comparison query operator:

比较操作符 对应 参数
$eq和$ne =和!= {:{$eq:}}
$gt和$gte >和>= {:{$gt:}}
$lt和$lte <和<= {:{$lt:}}
$in和$nin 包含 和 不包含 {:{$in:[,]}}

Example:


/*find age >22*/
db.user.find({"age":{$gt:22}})

Logical query operator:

逻辑操作符 对应 参数
$and {$and:[{条件1},..,{条件N}]} db.user.find({$and:[{"name":"tinyphp","num":3}]}) 等同 db.user.find({"name":"tinyphp","num":3})
$or {$or:[{条件1},..,{条件N}]}
$nor {$nor:[{条件1},..,{条件N}]}
$not 取反 {field:{$not:{条件}}}

Element operators:

元素操作符 作用
$exists 按照字段是否存在来查询文档 {field:{$exists:布尔值}} db.user.find({"age":{$exists:true}}) 查询存在age字段的文档
$type 选择字段值为指定BSON数据类型编号的文档

Regular match


/* find name  Beginning with j the */
db.user.find({"name":/^j/})

$where query

You can query in combination with javascript and return the current document only when javascript returns true


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

0

For queries, the $where operator cannot use an index. Each document needs to be converted from an BSON object to an javascript object before it can be run via the $where expression, so it is slower than regular queries and $where queries are generally avoided.

You can also save it for:


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

1

Update operation:

The overall updates


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

2

Local update

$inc modifier


/*update bing age+30 */
db.user.update({"name":"bing"},{$inc:{"age":30}})

$set modifier


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

4

The true parameter for update

If the last parameter of update is added with true, then when the modification condition does not exist, one will be added automatically, as follows:


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

5

One record is automatically added: name for mark, age for 10

If true is added and the condition is satisfied, it will be modified in batches, otherwise, only the first clause will be updated by default

Remove operation:


var single={"name":"tinyphp","num":28,}
for(var i=0;i<5;i++){single.num=i;db.user.insert(single);}

6

I hope that this article has been helpful to the MongoDB database programming.


Related articles: